import requests

def request(key, content, chatId):

    print('key', key)
    print('content', content)

    message = f"{content}"

    body = {
        "chatId": chatId,
        "model": "fast-gpt",
        "messages": [{"role": "user", "content": message}],
        "n": 1,
        "top_p": 1,
        "stop": None
    }

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {key}"
    }
    
    # 发送POST请求
    try:
        response = requests.post(
            # url="http://101.34.66.23:3000/api/openapi/v1/chat/completions",
            # http://121.40.228.116:3000//
            # url="http://121.36.111.223:3000/api/openapi/v1/chat/completions",
            url="http://121.40.228.116:3000/api/openapi/v1/chat/completions",
            json=body,
            headers=headers,
            timeout=180
        )
        response.raise_for_status()  # 如果请求失败，会抛出异常
        data = response.json()  # 获取响应内容
        try:
            res = data['choices'][0]['message']['content']
            return res
        except :
            res = data['message']
            print(f'-----------{res}--------------')
            return ''
    except requests.exceptions.RequestException as e:
        print(e)
        return None
